package com.yunhe.rentcars.pro.controller.permi;


import com.yunhe.rentcars.commons.dto.JSONResponse;
import com.yunhe.rentcars.commons.dto.Pageable;
import com.yunhe.rentcars.pro.entity.permi.Permission;
import com.yunhe.rentcars.pro.service.permi.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2017/10/19 0019.
 */

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    PermissionService permissionService ;


    /**
     * 列表页
     * @return
     */
       @RequestMapping
        public String list(){
            return "permi/permission/list";
        }

    @RequestMapping("/listData")
    @ResponseBody
    public Pageable list(int offset, int limit){
        Pageable<Permission> pageable = permissionService.findPage(offset, limit);
        return pageable;
    }

    @RequestMapping("/list/child")
    @ResponseBody
    public List<Permission> listByParent(Long parentid){
       Permission permission = new Permission();
       permission.setParentid(parentid);
        List<Permission> permissionList = permissionService.findPermisListByDan(permission);
        return permissionList;
    }
    /**
     * 添加页
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        return "permi/permission/add";
    }

    /**
     * 编辑页
     * @param id
     * @param req
     * @return
     */
    @RequestMapping("/edit")
    public ModelAndView edit(Long id, HttpServletRequest req){

        Permission permission = permissionService.find(id);
        ModelAndView mav = new ModelAndView("permi/permission/edit");

        if(null != permission && permission.getPermistype() > 1) {
            int type = (permission.getPermistype() == 2 ? 1 : 2);
            Permission permission1 = new Permission();
            permission1.setPermistype(type);
            List<Permission> parentList = permissionService.findPermisListByDan(permission1);
            mav.addObject("parentList", parentList);

        }
        mav.addObject("permission",permission);
        return mav;
    }

    /**
     * 添加操作
     * @param permission
     * @return
     */
    @RequestMapping("/save")
    public String save( Permission permission){
        permissionService.save(permission);
        return "redirect:/permission";
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    /*@RequestMapping("/delete")
    public String delete(Long id){
        permissionService.delete(id);
        return "redirect:/permission/list";
    }
*/

    /**
     * 删除操作
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResponse deleteBatch(String ids){
        permissionService.deleteBatch(ids);
        return JSONResponse.success("删除成功");
    }

    /**
     * 修改操作
     * @param permission
     * @return
     */
    @RequestMapping("/update")
    public String update(Permission permission){
            permissionService.update(permission);
            return "redirect:/permission";
    }


    @RequestMapping("/type")
    @ResponseBody
    public JSONResponse findListByType(Integer type){
            Permission permission = new Permission();
            permission.setPermistype(type);
            List<Permission> listByType = permissionService.findPermisListByDan(permission);
            return JSONResponse.success("成功",listByType);
    }
}
